package com.ryit.im.controller;


import com.ryit.im.base.PageParam;
import com.ryit.im.pojo.base.Result;
import com.ryit.im.pojo.dto.FriendApplyDto;
import com.ryit.im.pojo.dto.LoginDto;
import com.ryit.im.pojo.dto.RegisDto;
import com.ryit.im.pojo.dto.UserExtDto;
import com.ryit.im.service.ImService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    ImService service;

    @PostMapping("login")
    public Result login(@RequestBody LoginDto dto){
        return service.doLogin(dto);
    }

    @PostMapping("regis")
    public Result regis(@RequestBody RegisDto dto){
        return service.regis(dto);
    }

    @DeleteMapping("delFriend/{id}/{token}")
    public Result delFriend(@PathVariable String id,@PathVariable String token){
        return service.delFriend(id,token);
    }

    @PutMapping("pullBack/{id}/{token}")
    public Result pullBack(@PathVariable String id,@PathVariable String token){
        return service.pullBack(id,token);
    }

    @GetMapping("queryAppList")
    public Result queryAppList(PageParam page,String token){
        return service.queryAppList(page,token);
    }

    @GetMapping("queryContacts")
    public Result queryContacts(String gid,String name,String token){
        return service.queryContacts(gid,name,token);
    }

    @PostMapping("adoptApp")
    public Result adoptApp(@RequestBody FriendApplyDto dto){
        return service.adoptApp(dto);
    }

    @PutMapping("editInfo")
    public Result editInfo(@RequestBody UserExtDto dto){
        return service.editInfo(dto);
    }

    @GetMapping("queryUserOrGroup")
    public Result queryUserOrGroup(String id,String name,String token){
        return service.queryUserOrGroup(id,name,token);
    }

    @PostMapping("sendApp")
    public Result sendApp(@RequestBody FriendApplyDto dto){
        return service.sendApp(dto);
    }

    @GetMapping("userInfo/{id}/{token}")
    public Result userInfo(@PathVariable String id,@PathVariable String token){
        return service.userInfo(id,token);
    }

    @PutMapping("setRemark/{fid}/{name}/{token}")
    public Result setRemark(@PathVariable String fid,@PathVariable String name,@PathVariable String token){
        return service.setRemarks(fid,name,token);
    }

    @GetMapping("myQrCode/{token}")
    public void myQrCode(HttpServletResponse response,@PathVariable String token) throws IOException {
        Map<String,Object> str= service.myQrCode(token);
        if (str ==null){
            return;
        }
        InputStream is = (ByteArrayInputStream)str.get("inputStream");
        writeBytes(is,response.getOutputStream());
    }

    @GetMapping("queryAuthFromGroup/{gid}/{token}")
    public Result queryAuthFromGroup(@PathVariable String gid,@PathVariable String token){
        return service.getRole(gid,token);
    }

    @GetMapping("getGroupUserInfo/{id}/{gid}/{token}")
    public Result getGroupUserInfo(@PathVariable String id ,@PathVariable String gid,@PathVariable String token){
        return service.getGroupUserInfo(id,gid,token);
    }

    @GetMapping("removeGroupUser/{gid}/{token}")
    public Result removeGroupUser(@PathVariable String gid,@PathVariable String token){
        return service.removeGroupUser(gid,token);
    }

    @GetMapping("queryGroupAdmin/{gid}/{token}")
    public Result queryGroupAdmin(@PathVariable String gid,@PathVariable String token){
        return service.queryGroupAdmin(gid,token);
    }


    private void writeBytes(InputStream in, OutputStream out) throws IOException {
        byte[] buffer= new byte[1024];
        int length = -1;
        while ((length = in.read(buffer))!=-1){
            out.write(buffer,0,length);

        }
        in.close();
        out.close();
    }
}
